package com.dx.as.listener;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.dx.as.buriedpoint.bean.ActiveInfo;
import com.dx.as.buriedpoint.bean.AppInfo;
import com.dx.as.common.CommonUtils;
import com.dx.as.common.log.LogUtil;
import com.dx.as.common.sys.DeviceUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by jwaoo on 2017/6/19.
 */

public class ActivityLifecycleListener implements Application.ActivityLifecycleCallbacks {
    private Map<String,ActiveInfo> activeInfoMap = new HashMap<>();
    private List<Class<?>> activityList = new ArrayList();
    private AppInfo appInfo;
    private boolean isBackground = true;
    private boolean isComeBack = false;

    public boolean isBackground(){
        return isBackground;
    }

    public ActivityLifecycleListener(Context context) {
        if (appInfo == null) initAppInfo(context);
    }

    public List<Class<?>> getActivityList(){
        return activityList;
    }

    public boolean isCurrentActivity(Class<?> cls){
        if (activityList.size() > 0) return activityList.get(activityList.size()-1) == cls;
        return false;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        String name = activity.getClass().getSimpleName();
        isBackground = false;
        LogUtil.e("ActivityManager","create : "+name+activity.hashCode());
        if (!activeInfoMap.containsKey(name))
            activeInfoMap.put(name,getActiveInfo(name));
        else {
            LogUtil.e("ActivityManager", name + "  Created more than once");
        }
        if (activityList.contains(name)){
            List list = activityList.subList(activityList.indexOf(name)+1,activityList.size());
            activityList.removeAll(list);
        }else {
            activityList.add(activityList.size(),activity.getClass());
        }
    }

    @Override
    public void onActivityStarted(Activity activity) {
        if (isBackground){
            isBackground = false;
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {
        LogUtil.e("ActivityManager","Resumed : "+activity.getClass().getSimpleName());
        if (activityList.size() > 1 && activityList.get(activityList.size()-1) != activity.getClass()){
            isComeBack = true;
        }
    }

    @Override
    public void onActivityPaused(Activity activity) {
        LogUtil.e("ActivityManager","Paused : "+activity.getClass().getSimpleName());
    }

    @Override
    public void onActivityStopped(Activity activity) {
        String name = activity.getClass().getSimpleName();

        if (activityList.get(activityList.size()-1) == activity.getClass() && !isComeBack && !activity.getClass().getSimpleName().equals("PickImageActivity")){
            isBackground = true;
            isComeBack = true;
         //   ApplicationHelper.lastCls = activity.getClass();
            Intent intent = new Intent();
            intent.setAction(CommonUtils.getActionHomeDown());
            activity.sendBroadcast(intent);
            LogUtil.e("ActivityManager","stop : "+name);
        }
        isComeBack = false;
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        String name = activity.getClass().getSimpleName();
        LogUtil.e("ActivityManager","destroyed : "+name);
        activeInfoMap.remove(name);
        int index = activityList.lastIndexOf(activity.getClass());
        activityList.remove(index);
    }

    private void initAppInfo(Context context){
        appInfo = new AppInfo();
        int _id = (int)System.currentTimeMillis();
        long time = System.currentTimeMillis();
        appInfo.setStart_time(time);
        appInfo.set_id(_id);
        appInfo.setLast_version(DeviceUtil.getVersionName(context));
        appInfo.setEnd_time(time);
    }

    private ActiveInfo getActiveInfo(String name){

        ActiveInfo info = new ActiveInfo();
        int _id = (int)System.currentTimeMillis();
        info.set_id(_id);
        info.setPageName(name);
        long time = System.currentTimeMillis();
        info.setStart_time(time);
        info.setPreviousPage(activityList.size() == 0 ? "": activityList.get(activityList.size() - 1).getSimpleName());
        info.setDuration(0);
        info.setReStart_time(time);
        info.setHasSave(false);
//        info.setUuid(JWCache.getAccount());
        return info;
    }
}
